var app = getApp()
var verify = require('../../../../../utils/verify-token.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showModal:false,
    username: "",
    password:"",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var deviceid = options.deviceid
    var baseUrl = app.globalData.baseUrl
    var token = app.globalData.token
    var that = this

    this.setData({
      sh: app.globalData.screenHeight,
      sw: app.globalData.screenWidth,
      deviceid: deviceid,
    })
// 获取设备控制信息
    wx.request({
      url: baseUrl + 'device/getActions?deviceId=' + deviceid,
      header: {
        'AUTHORIZATION': token
      },
      success(res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          var deviceControl = res.data.data
          that.setData({
            deviceControl: deviceControl,
          })
        } else {
          // wx.showModal({
          //   title: '操作失败',
          //   content: res.data.message,
          // })
        }
        
      }
    })

    // 获取参数调节信息
    wx.request({
      url: baseUrl + 'device/param?deviceId=' + deviceid,
      header: {
        'AUTHORIZATION': token
      },
      success(res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          var paramControl = res.data.data
          // console.log("paramControl = ",paramControl)
          var paramList = []
          for (var i = 0; i < paramControl.length; i++) {
            paramList.push(paramControl[i].value)
            if (paramControl[i].type == "ENUM"){
              if (paramControl[i].valueView.indexOf("null")!=-1){
                paramControl[i].valueView = "点击选择"
              }
            }
          }
          that.setData({
            paramControl: paramControl,
            paramList: paramList
          })
        } else {
          // wx.showModal({
          //   title: '操作失败',
          //   content: res.data.message,
          // })
        }
        
      }
    })

    // 获取商品信息
    wx.request({
      url: baseUrl + 'device/' + deviceid + '/commodity_info',
      header: {
        'AUTHORIZATION': token
      },
      success(res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          var goodsList = res.data.data
          that.setData({
            goodsList: goodsList,
          })
        } else {
          // wx.showModal({
          //   title: '操作失败',
          //   content: res.data.message,
          // })
        }
        
      }
    })

  },
  // 设备操作
  devControl: function (res) {
    var deviceid = this.data.deviceid
    var deviceControl = this.data.deviceControl
    var index = res.currentTarget.dataset.index
    var actCode = deviceControl[index].actCode
    var baseUrl = app.globalData.baseUrl
    var token = app.globalData.token
    var that = this

    wx.request({
      url: baseUrl + 'device/action?deviceId=' + deviceid + '&actCode=' + actCode,
      method: "post",
      header: {
        'AUTHORIZATION': token
      },
      success(res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          wx.showToast({
            title: '操作成功!',
            duration: 3500
          })
        } else {
          wx.showModal({
            title: '操作失败！',
            content: res.data.message,
          })
        }
      }
    })
  },
  // 读取全部参数
  reLoadParam:function(){
    var deviceid = this.data.deviceid
    var baseUrl = app.globalData.baseUrl
    var token = app.globalData.token
    var that = this

    wx.request({
      url: baseUrl + 'device/param?deviceId=' + deviceid,
      header: {
        'AUTHORIZATION': token
      },
      success(res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          var paramControl = res.data.data
          var paramList = []
          for (var i = 0; i < paramControl.length; i++) {
            paramList.push(paramControl[i].value)
          }
          that.setData({
            paramControl: paramControl,
            paramList: paramList,
            pindex: -1,
          })
        } else {
          wx.showModal({
            title: '操作失败',
            content: res.data.message,
          })
        }
        
      }
    })
  },

  // 保存全部参数
  saveAllParam:function(){
    var deviceid = this.data.deviceid
    var paramList = this.data.paramList
    var paramControl = this.data.paramControl
    var baseUrl = app.globalData.baseUrl
    var token = app.globalData.token
    var that = this
    for (var i = 0; i < paramList.length;i++){
      if (paramControl[i].type == "NUMBER"){
        if (!/^(\-|\+)?\d+(\.\d+)?$/.test(paramList[i])) {
          wx.showModal({
            title: '格式错误',
            content: paramControl[i].name+'应该为数字类型！',
          })
          return;
        }
      }
    }

    wx.request({
      url: baseUrl + 'device/all_param?deviceId=' + deviceid + '&values=' + paramList,
      method:"put",
      header: {
        'AUTHORIZATION': token
      },
      success(res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          wx.showToast({
            title: '保存成功!',
            duration: 3500
          })
          that.setData({
            pindex:-1
          })
        } else {
          wx.showModal({
            title: '保存失败！',
            content: res.data.message,
          })
        }
      }
    })
  },

  // 参数调节输入框聚焦
  focusInput:function(res){
    var pindex = res.currentTarget.dataset.index
    this.setData({
      pindex:pindex
    })
  },
  // 参数输入框
  paramInput:function(res){
    var pindex = res.currentTarget.dataset.index
    var pvalue = res.detail.value
    var paramList = this.data.paramList
    paramList[pindex] = pvalue
    this.setData({
      paramList: paramList
    })
  },
  // 选择参数
  select:function(res){
    var index = res.currentTarget.dataset.index
    var title = res.currentTarget.dataset.title+"调节"
    var values = res.currentTarget.dataset.values
    var pvalue = res.currentTarget.dataset.value
    var list = []
    var iList = []
    for(var key in values){
      list.push(values[key])
      iList.push(key)
    }
    for(var i = 0;i<iList.length;i++){
      if(pvalue==iList[i]){
        pvalue=i
        break
      }
    }
    this.setData({
      showModal:true,
      title:title,
      list:list,
      iList: iList,
      pvalue:pvalue,    //
      pindex: index,   //pindex代表要调节哪个条目
    })
  },

  editItem:function(res){
    var pvalue = res.currentTarget.dataset.value
    this.setData({
      pvalue: pvalue
    })
  },
  // 确定
  confirm:function(){
    var pindex = this.data.pindex
    var pvalue = this.data.pvalue
    console.log("pvalue = ", pvalue)
    var paramList = this.data.paramList
    var paramControl = this.data.paramControl
    var list = this.data.list
    var iList = this.data.iList
    paramList[pindex] = iList[pvalue]*1
    paramControl[pindex].valueView = iList[pvalue]+"-"+list[pvalue]
    if (paramControl[pindex].valueView.indexOf("undefined")!=-1){
      paramControl[pindex].valueView = "点击选择"
    }
    paramControl[pindex].value = pvalue
    this.setData({
      showModal: false,
      paramList: paramList,
      paramControl: paramControl,
    })
  },
  // 取消
  cancel:function(){
    this.setData({
      showModal: false
    })
  },

  // 输入用户名
  editusername:function(res){
    var username = res.detail.value
    this.setData({
      username:username
    })
  },
  // 输入密码
  editpassword: function (res) {
    var password = res.detail.value
    this.setData({
      password:password
    })
  },
  // 获取账号密码
  getUser:function(){
    var deviceid = this.data.deviceid
    var baseUrl = app.globalData.baseUrl
    var token = app.globalData.token
    var that = this

    wx.request({
      url: baseUrl + 'deviceGetPswd?deviceId=' + deviceid,
      header: {
        'AUTHORIZATION': token
      },
      success(res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {

        } else {
          wx.showModal({
            title: '操作失败',
            content: res.data.message,
          })
        }
        var username = res.data.data.user
        var password = res.data.data.pswd
        that.setData({
          username:username,
          password:password
        })
        
      }
    })
  },

  // 保存用户名密码
  saveUser: function () {
    var deviceid = this.data.deviceid
    var username = this.data.username
    var password = this.data.password
    var baseUrl = app.globalData.baseUrl
    var token = app.globalData.token
    var that = this

    if (!/^\w{6,18}$/.test(password) || !/^\w{6,18}$/.test(username)) {
      wx.showModal({
        title: '格式错误',
        content: '账号或密码只能是6-18位英文字母或数字组成',
      })
      return;
    }

    wx.request({
      url: baseUrl + 'device_set_admin?deviceId=' + deviceid + '&user=' + username + '&pswd=' + password,
      method:"put",
      header: {
        'AUTHORIZATION': token
      },
      success(res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          wx.showToast({
            title: '保存成功!',
            duration: 3500
          })
        } else {
          wx.showModal({
            title: '保存失败！',
            content: res.data.message,
          })
        }
      }
    })
  },

  // 制作饮品
  // make:function(){
  //   var deviceid = this.data.deviceid
  //   var actCode = 51
  //   var baseUrl = app.globalData.baseUrl
  //   var token = app.globalData.token
  //   var that = this

  //   wx.request({
  //     url: baseUrl + 'device/action?deviceId=' + deviceid + '&actCode=' + actCode,
  //     method: "post",
  //     header: {
  //       'AUTHORIZATION': token
  //     },
  //     success(res) {
  //       if (res.data.code == 60006) {
  //         verify.clearAll()
  //       } else if (res.data.code == 200) {
  //         wx.showToast({
  //           title: '操作成功!',
  //           duration: 3500
  //         })
  //       } else {
  //         wx.showModal({
  //           title: '操作失败！',
  //           content: res.data.message,
  //         })
  //       }
  //     }
  //   })
  // },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})